var awPlus = {
  // properties -----------------
  base_url: null,
  doc: null,
  prefs: Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch),
  
  // Methods --------------------
  init: function(e) {
    if (awPlus.prefs.getBoolPref('awplus.disabled')) return;
    
    var location = e.originalTarget.location.toString();
    
    if (!/^http:\/\/www1\.astrowars\.com/.test(location)) {
      if (/^http:\/\/aw\.freeglossary\.com/.test(location)) {
        awPlus.base_url = 'http://aw.freeglossary.com';
      }
      else return;
    }
    else awPlus.base_url = 'http://www1.astrowars.com';

    awPlus.doc = e.originalTarget;
  
    if (/\.com\/0\/.*$/.test(location)) {
      awPlus.add_script("libs/jquery-1.3.2.js");
      awPlus.add_script("libs/jquery-ui-1.7.2.js");
      awPlus.add_script("libs/awplus.js");
      awPlus.add_script("plugins/systemplayer_dialog.js");
      
      var theme = awPlus.prefs.getCharPref('awplus.theme');
      awPlus.loadTheme(theme);
      
      if (/News\/(\/\?p=\d+)?$/.test(location)) {
        awPlus.add_script("plugins/news.js");
      }
      else if (/Map/.test(location)) {
        awPlus.add_script("plugins/map.js");
      }
      else if (/Planets\/$/.test(location)) {
        awPlus.add_script("plugins/planet.js");
      }
      else if (/Planets\/Detail.php\/\?i=[0-9]+$/.test(location)) {
        awPlus.add_script("plugins/planetdet_levelup.js");
        awPlus.add_script("plugins/planetdet_fleets.js");
      }
      else if (/Planets\/Buildings.php$/.test(location)) {
        awPlus.add_script("plugins/planetbuild_possible.js");
      }
      else if (/\/Science\/$/.test(location)) {
        awPlus.add_script("plugins/science.js");
      }
      else if (/\/Trade\/$/.test(location)) {
        if (awPlus.prefs.getBoolPref('awplus.trade_quick_buy')) {
          awPlus.add_script("plugins/trade.js");
        }
    	  if (awPlus.prefs.getBoolPref('awplus.trade_prices')) {
          awPlus.add_script("plugins/trade_prices.js");
        }
      }
      else if (/Alliance\/$/.test(location)) {
        awPlus.add_script('plugins/alliance.js');
      }
      else if (/Alliance\/List.php$/.test(location)) {
        awPlus.add_script('plugins/alliance_list.js');
      }
      else if (/Alliance\/Detail.php\/\?id=[0-9]+$/.test(location)) {
        awPlus.add_script('plugins/alliance_memberlist.js');
        awPlus.add_script('plugins/alliance_fleetscv.js');
      }
      else if (/Fleet\/$/.test(location)) {
        awPlus.add_script('plugins/fleets.js');
      }
      else if (/Launch\.php/.test(location)) {
        awPlus.add_script("plugins/launch_systemonselect.js");
        awPlus.add_script("plugins/launch_traveltime.js");
        if (awPlus.prefs.getBoolPref('awplus.launch_check')) {
          awPlus.add_script("plugins/launch_check.js");
        }
        awPlus.add_script("plugins/launch_quickdestinations.js");
        awPlus.add_script("plugins/launch_fleetparcels.js");
      }
    }
    
    awPlus.add_script("plugins/search_bottom_bar.js");
    awPlus.write_js("awPlus.base_url = '"+ awPlus.base_url +"';");
    awPlus.add_script("libs/awplus_finalizer.js");
  },
  
  initPrefs: function() {
    document.getElementById('apDisabled').setAttribute("checked", awPlus.prefs.getBoolPref('awplus.disabled'));
    document.getElementById('apLaunchCheck').setAttribute("checked", awPlus.prefs.getBoolPref('awplus.launch_check'));
    document.getElementById('apTradeQuickBuy').setAttribute("checked", awPlus.prefs.getBoolPref('awplus.trade_quick_buy'));
    document.getElementById('apTradePrices').setAttribute("checked", awPlus.prefs.getBoolPref('awplus.trade_prices'));
    awPlus.switchCheckedTheme();
  },
  
  add_script: function(aURL) {
    aURL = "chrome://awplus/content/" + aURL;
    var text = awPlus.getContents(aURL) + "\n\n";
    
    awPlus.write_js(text);
  },
  
  write_js: function(src) {
    var scriptElm = awPlus.doc.createElement("script");
    scriptElm.appendChild(awPlus.doc.createTextNode(src));
    awPlus.doc.body.appendChild(scriptElm);
    awPlus.doc.body.removeChild(scriptElm);
  },

  add_css: function(aURL) {
    var linkElm = awPlus.doc.createElement("link");
    linkElm.rel = 'stylesheet';
    linkElm.type = 'text/css';
    linkElm.href= aURL;
    awPlus.doc.body.appendChild(linkElm);
  },
  
  loadTheme: function(theme) {
    awPlus.add_css('http://awplus.googlecode.com/svn/branches/themes/css/'+theme+'/jquery-ui-1.7.1.custom.css');
  },

  getContents: function(aURL){
    var ioService=Components.classes["@mozilla.org/network/io-service;1"]
      .getService(Components.interfaces.nsIIOService);
    var scriptableStream=Components
      .classes["@mozilla.org/scriptableinputstream;1"]
      .getService(Components.interfaces.nsIScriptableInputStream);
    var channel=ioService.newChannel(aURL,null,null);
    try {
    var input=channel.open();
    scriptableStream.init(input);
    var str=scriptableStream.read(input.available());
    scriptableStream.close();
    input.close();
    }
    catch (err) {
      alert('awPlus Error: File not found! ('+aURL+')');
    }
    return str;
  },

  toggleAwPlus: function(o) {
    var option = o.getAttribute("checked") ? true : false;
    awPlus.prefs.setBoolPref('awplus.disabled', option);
  },

  toggleAwPlusItens: function(o, p) {
    var option = o.getAttribute("checked") ? true : false;
    awPlus.prefs.setBoolPref(p, option);
  },

  openAwUrl: function(url) {
    var u = awPlus.base_url + url;
    awPlus.openUrl(u);
  },

  openUrl: function(url) {
    gBrowser.loadURI(url);
  },
  
  changeTheme: function (o, t) {
    awPlus.prefs.setCharPref('awplus.theme', t);
    awPlus.loadTheme(t);
    awPlus.switchCheckedTheme();
  },
  
  switchCheckedTheme: function () {
    var menu = document.getElementById('apThemesMenu');
    var item;

    for (var i = 0; i < menu.itemCount; i++) {
      item = menu.getItemAtIndex(i);
      item.setAttribute("checked", false);
    }
    var theme = awPlus.prefs.getCharPref('awplus.theme');
    document.getElementById('apTheme-'+ theme).setAttribute("checked", true);
  }
};



window.addEventListener("load", function() {
  awPlus.initPrefs();
  var appcontent = window.document.getElementById("appcontent");
  if (appcontent) {
    if (!appcontent.greased_awplus) {
      appcontent.greased_awplus = true;
      appcontent.addEventListener("DOMContentLoaded", awPlus.init, false);
    }
  }
}, false);
